<template>
  <div class="login-container">
    <div class="login-background">
      <!-- 左侧品牌展示区 -->
      <div class="brand-section">
        <div class="brand-content">
          <div class="brand-logo">
            <i class="icon-car">🚗</i>
            <h1>AutoParts Pro</h1>
          </div>
          <p class="brand-slogan">专业汽车配件，品质保证</p>
          <div class="feature-list">
            <div class="feature-item">
              <i class="icon">⚡</i>
              <span>正品保障</span>
            </div>
            <div class="feature-item">
              <i class="icon">🚚</i>
              <span>快速配送</span>
            </div>
            <div class="feature-item">
              <i class="icon">🔧</i>
              <span>专业服务</span>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 右侧登录表单 -->
      <div class="login-section">
        <div class="login-card">
          <div class="login-header">
            <h2 class="login-title">欢迎回来</h2>
            <p class="login-subtitle">登录您的账户</p>
          </div>
          
          <el-form :model="form" class="login-form" :rules="rules" ref="loginFormRef">
            <el-form-item prop="username">
              <el-input 
                v-model="form.username" 
                placeholder="请输入用户名" 
                size="large"
                prefix-icon="User"
                class="custom-input"
              />
            </el-form-item>
            
            <el-form-item prop="password">
              <el-input 
                v-model="form.password" 
                type="password" 
                placeholder="请输入密码" 
                size="large"
                prefix-icon="Lock"
                show-password
                class="custom-input"
                @keyup.enter="handleLogin"
              />
            </el-form-item>
            
            <div class="login-options">
              <el-checkbox v-model="rememberMe">记住我</el-checkbox>
              <el-link type="primary">忘记密码？</el-link>
            </div>
            
            <el-button 
              type="primary" 
              size="large" 
              @click="handleLogin" 
              :loading="loading"
              class="login-btn"
            >
              登录
            </el-button>
            
            <div class="login-footer">
              <span>还没有账户？</span>
              <el-link type="primary" @click="$router.push('/register')">立即注册</el-link>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { login } from '@/api/user'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()
const loginFormRef = ref<FormInstance>()

const form = reactive({
  username: '',
  password: ''
})

const rules = reactive<FormRules>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ]
})

const loading = ref(false)
const rememberMe = ref(false)

onMounted(() => {
  // 检查本地存储的记住我信息
  const savedUsername = localStorage.getItem('rememberedUsername')
  if (savedUsername) {
    form.username = savedUsername
    rememberMe.value = true
  }
})

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  const valid = await loginFormRef.value.validate()
  if (!valid) return

  loading.value = true
  try {
    const result = await login(form)
    
    // 保存用户信息和token
    userStore.setToken(result.data.token)
    userStore.setUserInfo(result.data.userInfo)
    
    // 处理记住我功能
    if (rememberMe.value) {
      localStorage.setItem('rememberedUsername', form.username)
    } else {
      localStorage.removeItem('rememberedUsername')
    }
    
    ElMessage.success('登录成功')
    router.push('/')
  } catch (error: any) {
    ElMessage.error(error.message || '登录失败')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #1e3a8a 0%, #3b82f6 50%, #60a5fa 100%);
}

.login-background {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 1fr 1fr;
  align-items: center;
}

/* 左侧品牌展示区 */
.brand-section {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(20px);
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  border-right: 1px solid rgba(255, 255, 255, 0.2);
}

.brand-content {
  text-align: center;
  color: white;
  padding: 0 40px;
}

.brand-logo {
  margin-bottom: 32px;
}

.brand-logo .icon-car {
  font-size: 64px;
  display: block;
  margin-bottom: 16px;
}

.brand-logo h1 {
  font-size: 36px;
  font-weight: 700;
  margin: 0;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

.brand-slogan {
  font-size: 18px;
  opacity: 0.9;
  margin-bottom: 40px;
}

.feature-list {
  display: flex;
  gap: 32px;
  justify-content: center;
}

.feature-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.feature-item .icon {
  font-size: 24px;
}

.feature-item span {
  font-size: 14px;
  opacity: 0.8;
}

/* 右侧登录表单 */
.login-section {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.login-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 20px;
  padding: 48px 40px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  width: 100%;
  max-width: 420px;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-title {
  font-size: 28px;
  font-weight: 600;
  color: #1e293b;
  margin-bottom: 8px;
}

.login-subtitle {
  color: #64748b;
  font-size: 16px;
}

.login-form {
  .custom-input {
    :deep(.el-input__wrapper) {
      background: rgba(255, 255, 255, 0.9);
      border-radius: 12px;
      box-shadow: 0 2px 12px rgba(30, 58, 138, 0.1);
      border: 1px solid rgba(30, 58, 138, 0.2);
      transition: all 0.3s ease;
    }
    
    :deep(.el-input__wrapper:hover) {
      border-color: #3b82f6;
      box-shadow: 0 4px 16px rgba(59, 130, 246, 0.2);
    }
    
    :deep(.el-input__wrapper.is-focus) {
      border-color: #3b82f6;
      box-shadow: 0 4px 20px rgba(59, 130, 246, 0.3);
    }
  }
}

.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 16px 0 24px;
}

.login-btn {
  width: 100%;
  height: 48px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #3b82f6 0%, #1e40af 100%);
  border: none;
  transition: all 0.3s ease;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(59, 130, 246, 0.4);
}

.login-footer {
  text-align: center;
  margin-top: 24px;
  color: #64748b;
  font-size: 14px;
}

.login-footer .el-link {
  margin-left: 4px;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .login-background {
    grid-template-columns: 1fr;
  }
  
  .brand-section {
    display: none;
  }
  
  .login-section {
    padding: 20px;
  }
}

@media (max-width: 480px) {
  .login-card {
    padding: 32px 24px;
    margin: 0 16px;
  }
  
  .login-title {
    font-size: 24px;
  }
  
  .login-options {
    flex-direction: column;
    gap: 12px;
    align-items: flex-start;
  }
}
</style>